home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
Apple Script
/
OSAX
/
Four Password OSAXen
/
Password1 Folder
/
PassWord.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-05
|
3KB
|
133 lines
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Password.c
// Written by: Donald Olson
// May 1993
// Copyright ® 1993 Apple Computer Inc.
// All rights reserved.
//
// This is a simple Scripting Addition that asks the user to type in a
// password. This was written for a talk at the 1993 WWDC given
// by Donald Olson and Donn Denman.
//
// Syntax: User Password
// Return: Encrypted form of user password or cancel.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <string.h>
#include <Dialogs.h>
#include <AppleEvents.h>
//AEVTOLIEpswd
// Our headers
void FrameDefault( DialogPtr theDialog, long theItem);
pascal Boolean MyDlgFilter( DialogPtr theDlg,
EventRecord *theEventRec, short *itemHit);
void EncryptString(StringPtr strPtr);
// Our constant
#define DLOGID 128
pascal OSErr main(AppleEvent *theEvent,
AppleEvent *theReply,
long theRefCon)
{
DialogPtr theDialog = nil;
OSErr theErr = noErr;
GrafPtr savedPort;
short itemHit;
Str32 theString;
StringPtr theStrPtr = (StringPtr)&theString;
StringHandle theStrHdl = (StringHandle)&theStrPtr;
GetPort(&savedPort);
theErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
if(theErr != noErr) return theErr;
theDialog = GetNewDialog(DLOGID, nil, (WindowPtr)-1);
if(theDialog == nil) return resNotFound;
SetPort(theDialog);
FrameDefault(theDialog, ok);
strcpy((char*)theString, (char*) "\0"); // Empty string
/*
Use window refCon to pass user input back to this routine
since the text field will have our bullets instead of the
actual text entered.
*/
SetWRefCon(theDialog, (long)theStrHdl);
do {
ModalDialog((ModalFilterProcPtr)MyDlgFilter, &itemHit);
} while (itemHit != cancel && itemHit != ok);
if(itemHit == cancel) {
DisposDialog(theDialog);
return userCanceledErr;
}
DisposDialog(theDialog);
EncryptString(theStrPtr);
theErr = AEPutParamPtr(theReply, keyDirectObject, typeChar,
(Ptr)theString, strlen((char*)theString));
SetPort(savedPort);
return theErr;
}
void EncryptString(StringPtr strPtr) {
short theSize, x;
theSize = strlen((char*)(strPtr));
for( x = 0; x <= (theSize -1); x++) {
strPtr[x] = (short)strPtr[x] + (short)"\¡";
}
}
pascal Boolean MyDlgFilter(DialogPtr theDlg, EventRecord *theEventRec, short *itemHit) {
StringHandle theStrHdl;
short theSize;
if(theEventRec->what != keyDown) // Just looking for keystrokes
return false;
switch((theEventRec->message) & charCodeMask)
{
case 0x0d: // Return
*itemHit = ok;
return true;
case 0x03: // Enter
*itemHit = ok;
return true;
case 0x08: // Backspace
return false;
default:
theStrHdl = (StringHandle)GetWRefCon(theDlg);
theSize = strlen((char*)(*theStrHdl));
(*theStrHdl)[theSize] = (char)(theEventRec->message);
(*theStrHdl)[theSize + 1] = (char)NULL;
theEventRec->message = '•';
return false;
}
}
void FrameDefault( DialogPtr theDialog, long theItem) {
short DType;
Handle DItem;
Rect DRect;
GetDItem(theDialog, theItem, &DType, &DItem, &DRect);
PenSize(3, 3);
InsetRect(&DRect, -4, -4);
FrameRoundRect(&DRect, 16, 16);
PenSize(1, 1);
}